﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
// 原题连接：https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
/*
题目描述：
用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，
分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )

 

示例 1：
输入：
["CQueue","appendTail","deleteHead","deleteHead","deleteHead"]
[[],[3],[],[],[]]
输出：[null,null,3,-1,-1]

示例 2：
输入：
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出：[null,-1,null,null,5,2]

提示：
1 <= values <= 10000
最多会对 appendTail、deleteHead 进行 10000 次调用
*/

// 开始解题：
// 先将栈实现一下





// 重定义数据类型
typedef int DataType;

// 定义栈结构
typedef struct stack {
	DataType* data;
	int top;
	int capacity;
} Stack;

// 栈的初始化
void StackInit(Stack* ps);

// 压栈
void StackPush(Stack* ps, DataType x);
// 弹栈
void StackPop(Stack* ps);
// 返回栈顶数据
DataType StackTop(Stack* ps);
// 返回栈的数据个数
int StackSize(Stack* ps);
// 判断栈是否为空
bool StackEmpty(Stack* ps);
// 栈的销毁
void DestroyStack(Stack* ps);

// 栈的初始化
void StackInit(Stack* ps) {
	assert(ps);
	ps->data = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

// 压栈
void StackPush(Stack* ps, DataType x) {
	assert(ps);
	// 检查是否需要增容
	if (ps->top == ps->capacity) {
		int newCapacity = ps->capacity == 0 ? 10 : ps->capacity * 2;
		DataType* temp = (DataType*)realloc(ps->data, newCapacity * sizeof(DataType));
		if (NULL == temp) {
			perror("ralloc fail!\n");
			exit(-1);
		}
		ps->data = temp;
		ps->capacity = newCapacity;
	}
	ps->data[ps->top] = x;
	ps->top++;
}

// 弹栈
void StackPop(Stack* ps) {
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}

// 返回栈顶数据
DataType StackTop(Stack* ps) {
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->data[ps->top - 1];
}

// 返回栈的数据个数
int StackSize(Stack* ps) {
	assert(ps);
	assert(ps->top >= 0);
	return ps->top;
}

// 判断栈是否为空
bool StackEmpty(Stack* ps) {
	assert(ps);
	return ps->top == 0;
}

// 栈的销毁
void DestroyStack(Stack* ps) {
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->top = 0;
	ps->capacity = 0;
}


typedef struct {
	Stack headStack; // 队头栈，用于出队
	Stack tailStack; // 队尾栈，用于入队
} CQueue;


CQueue* cQueueCreate() {
	CQueue* queue = (CQueue*)malloc(sizeof(CQueue));
	if (NULL == queue) {
		perror("malloc fail!\n");
		exit(-1);
	}
	StackInit(&queue->headStack);
	StackInit(&queue->tailStack);
	return queue;
}

void cQueueAppendTail(CQueue* obj, int value) {
	StackPush(&obj->tailStack, value);
}

int cQueueDeleteHead(CQueue* obj) {
	if (StackEmpty(&obj->headStack) && StackEmpty(&obj->tailStack)) {
		return -1;
	}
	if (StackEmpty(&obj->headStack)) {
		// 先将队头栈的数据全都压到队尾栈
		while (!StackEmpty(&obj->tailStack)) {
			StackPush(&obj->headStack, StackTop(&obj->tailStack));
			StackPop(&obj->tailStack);
		}
	}
	int result = StackTop(&obj->headStack);
	StackPop(&obj->headStack);
	return result;
}

void cQueueFree(CQueue* obj) {
	if (!(StackEmpty(&obj->headStack) && StackEmpty(&obj->tailStack))) {
		DestroyStack(&obj->headStack);
		DestroyStack(&obj->tailStack);
	}
	free(obj);
}